In Python, the assignment statement (= operator) does not copy objects. Instead, it creates a binding between the existing object and the target variable name. To create copies of an object in Python, we need to use the copy module. Moreover, there are two ways of creating copies for the given object using the copy module -
• Shallow Copy is a bit-wise copy of an object. The copied object created has an exact copy of the values in the original object. If either of the values are references to other objects, just the reference addresses for the same are copied.
• Deep Copy copies all values recursively from source to target object, i.e. it even duplicates the objects referenced by the source object.
from copy import copy, deepcopy
list_1 = [1, 2, [3, 5], 4]
## shallow copy
list_2 = copy(list_1)
list_2[3] = 7
list_2[2].append(6)
list_2 # output => [1, 2, [3, 5, 6], 7]
list_1 # output => [1, 2, [3, 5, 6], 4]
## deep copy
list_3 = deepcopy(list_1)
list_3[3] = 8
list_3[2].append(7)
list_3 # output => [1, 2, [3, 5, 6, 7], 8]
list_1 # output => [1, 2, [3, 5, 6], 4]
Posted Date:- 2021-08-16 06:02:15
How do you change the data type of a list?
What are the tools that help to find bugs or perform static analysis?
What do you understand by Tkinter?
Do we need to declare variables with data types in Python?
What type of language is python? Programming or scripting?
Is indentation required in Python?
What are the common built-in data types in Python?
What is, not and in operators?
How to capitalize the first letter of string?
What is the split function used for?
What is the difference between Xrange and range?
How can you access a module written in Python from C?
How can you generate random numbers in Python?
Mention what is Flask-WTF and what are their features?
Five benefits of using Python?
Explain what is Dogpile effect? How can you prevent this effect?
Explain how you can access sessions in Flask?
Explain what is the common way for the Flask script to work?
Is python numpy better than lists?
What is map function in Python?
What do you understand by monkey patching in Python?
Why do we need membership operators in Python?
How To Save An Image Locally Using Python Whose URL Address I Already Know?
How do you delete a file in Python?
How to check Python Version in CMD?
Write a sorting algorithm for a numerical dataset in Python.
How many ways can be applied for applying reverse string?
Why do we need a break in Python?
Can we reverse a list in Python?
How do you get a list of all the keys in a dictionary?
Does python support multiple inheritance?
What can be the length of the identifier in Python?
What are the supported standard data types in Python?
Why do we use Pythonstartup environment variable?
How can you pick a random item from a range?
Why do we use join() function in Python?
What is a dictionary in Python?
How will you convert a string to all lowercase?
What is zip() function in Python?
What is pickling and unpickling?
What are Literals in Python and explain about different Literals?
How do you copy an object in Python?
What are lists and tuples? What is the key difference between the two?
Python an interpreted language. Explain.
How is memory managed in Python?
What type of language is python? Programming or scripting?